/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.entity;

import com.bifrost.constant.RoleType;
import com.fasterxml.jackson.annotation.JsonBackReference;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 *角色组
 *
 *@author joewee
 *@version 1.0.0
 *@Date 2018/2/25 21:46
 */
@Entity
@Table(name = "ROLE")
@NamedQuery(name = "Role.findAll", query = "SELECT p FROM Role p ")
@Getter
@Setter
@Builder
@AllArgsConstructor
public class Role extends BaseEntity implements Serializable{
    @Id
    @Column(name = "ROLE_ID", unique=true, nullable=false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column (name ="ROLE_TYPE")
    @Enumerated(value = EnumType.STRING)
    private RoleType roleType;

    @Column(name="ROLE_NAME", unique=true)
    private String roleName;

    @Column(name = "ROLE_DESC")
    private String description;

    @ManyToMany(cascade = {CascadeType.PERSIST})
    @JoinTable(
            name = "ROLE_PERMISSION",
            joinColumns = @JoinColumn(name = "ROLE_ID"),
            inverseJoinColumns = @JoinColumn(name="PERMISSION_ID")
    )
    @OrderBy("type,order ASC")
    private Set<Permission> permissions = new HashSet<Permission>();

    @ManyToMany(mappedBy = "roles",cascade = {CascadeType.PERSIST})
    @JsonBackReference
    private Set<User> users = new HashSet<>();

    //=====================
    public Role() {

    }

    public Role(String roleName) {
        this.roleName = roleName;
    }

}
